﻿using Modbus.Device;
using PLC.Base;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLC.Device
{
    /// <summary>
    /// 8902F2的数据读取
    /// </summary>
    class ModBusRTUManager : IDeviceReader
    {
        #region 私有变量
        SerialPort serialPort;
        ModbusSerialMaster master;

        private  String portName = "COM1";
        private  int baudRate = 9600;
        private  Parity parity = Parity.None;
        private  ushort dataBits = 8;
        private  StopBits stopBits = StopBits.One;
        private  int readTimeOut = 3000;
        private Boolean isOpen = false;
        //private double transformationRatio = 1.0f;
        #endregion

        #region 私有函数
        public ModBusRTUManager(String portName, int baudRate, Parity parity, ushort dataBits, StopBits stopBits, int readTimeOut)
        {
            this.portName = portName;
            this.baudRate = baudRate;
            this.parity = parity;
            this.dataBits = dataBits;
            this.stopBits = stopBits;
            this.readTimeOut = readTimeOut;


            serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
            serialPort.ReadTimeout = readTimeOut;
            master = ModbusSerialMaster.CreateRtu(serialPort);

        }

        //int phaseAdress;


        private ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            ushort[] registerBuffer;
            registerBuffer = master.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);

            return registerBuffer;
        }
        #endregion

        #region 接口实现
        public bool IsOpen { get => isOpen; set => isOpen = value; }
       // public int PhaseAdress { get => phaseAdress; set => phaseAdress = value; }

        public int Open()
        {
            if (!isOpen)
            {
                serialPort.Open();
                
                isOpen = true;
            }
            
            return 0;
        }

        public void Close()
        {
            if (isOpen)
            {
                serialPort.Close();
                isOpen = false;
            }
        }

        private double getValue(ushort data1, ushort data2)
        {
            byte[] b = new byte[4];
            b[0] = (byte)(data1 >> 8);
            b[1] = (byte)data1;
            b[2] = (byte)(data2 >> 8);
            b[3] = (byte)data2;
            /// byte[] byteTemp = new byte[4] { b[0], b[1], b[2], b[3] };
            byte[] byteTemp = new byte[4] { b[3], b[2], b[1], b[0] };

            return (double)BitConverter.ToSingle(byteTemp, 0);
        }

        public EnvData ReadData(DynamicParams dynamicParams)
        {
            double transformationRatio = dynamicParams.TransformationRatio;
            ushort phaseAdress = dynamicParams.PhaseAdress;

            ushort[] meanData = ReadHoldingRegisters(1, phaseAdress, 20);

            EnvData data = new EnvData();

            data.Voltage =     getValue(meanData[0], meanData[1]);
            data.Current =     getValue(meanData[2], meanData[3]) * transformationRatio;
            data.ShaftPower =  getValue(meanData[4], meanData[5]) * transformationRatio;
            data.Powerfactor = getValue(meanData[10], meanData[11]);

            return data;


        }
        #endregion


    }
}
